(function(window, $) {
	var role = window.role = function() {
	};
	role.tableColOpt = function(value, row, index, field) {
        var grant = '<button type="button" onclick="role.grant(\'' + value + '\')" class="btn btn-success btn-sm"><i class="fa fa-flash"></i> 授权</button>',
            defBtn = base.tableColOpt(value, row, index, field);
        return [grant, defBtn].join(' ');
    };
    role.grant = function(id) {
		$('#menu-tree').data('id', id);
		$('#grant-modal').modal('toggle');
		$.getJSON('grant?id=' + id, function(rs) {
            var setting={
                check: {
                    enable: true
                },
                data: {
                    key: {
                       url: '#',
                       treeNode: 'icon'
                    },
                    simpleData: {
                        enable: true,
                        idKey: "id",
                        pIdKey: "pid"//,
//                        rootPId: 0
                    }
                }
            }
            var tree = $.fn.zTree.init($("#menu-tree"), setting, rs.data.menus);
            tree.expandAll(true);
            $(rs.data.roleMenus).each(function(){
            	var node = tree.getNodesByParam('id', this.menuId);
                node && node[0] && tree.checkNode(node[0], true, false);
            });
		});
		return false;
	}
	$(function(){
		$('#grant-save').click(grantSave);
	});
	function grantSave(){
		var nodes = $.fn.zTree.getZTreeObj("menu-tree").getCheckedNodes(true);
		var data = [];
        $(nodes).each(function () {
            data.push({name: 'menuId', value: this.id});
        });
        data.push({name: 'roleId', value: $('#menu-tree').data('id')});
        $.getJSON('saveGrant', data, base.ajaxBack);
	}
})(this, jQuery);